package com.yfbao.horizon.demo;

import com.alibaba.druid.util.JdbcUtils;
import com.yfbao.horizon.utils.JdbcUtils_druid;
import javassist.bytecode.analysis.Executor;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MutilThreadQuery {


    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://rm-2ze3b285g5s3lghsa.mysql.rds.aliyuncs.com:3306/mydb";
    private static final String USERNAME = "seeyonv8_db_view";
    private static final String PASSWORD = "Seeyon123456";

    // 单例模式，保证全局只有一个数据库连接实例
    private static Connection connection = null;

    // 获取数据库连接实例
    public static Connection getInstance() throws ClassNotFoundException, SQLException {
        if (connection == null) {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        }
        return connection;
    }

    public static void testQuery(String sql){
        Connection connection = null;
        Statement statement = null;
        try {
            connection = JdbcUtils_druid.getConnection();
            // 获取数据库连接实例
            // 创建Statement对象
            statement = connection.createStatement();
            // 执行SQL语句
            statement.executeQuery(sql);
            // 关闭Statement对象
            statement.close();
        } catch ( SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_druid.release(connection,statement,null);
        }
    }

    public static void main(String[] args) {
        testMutiQuery();
    }

    private static void testMutiQuery(){
        List<String> queryList = new ArrayList<>();
        queryList.add("SELECT COUNT(*) FROM org_job WHERE org_id = -1730833917365171641 \tAND business_id = -5038278851089511626 \tAND (name = '性能职务36d58d03-7eab-4bd2-86e8-3615acf0594d'   OR name LIKE '%\"zh_CN\":\"性能职务36d58d03-7eab-4bd2-86e8-3615acf0594d\"%'   OR name LIKE '%\"zh_TW\":null%'   OR name LIKE '%\"en\":null%') \tAND org_job.tenant_id = -1");
        queryList.add("SELECT COUNT(*) FROM org_job WHERE org_id = -1730833917365171641 \tAND business_id = -5038278851089511626 \tAND (name = '性能职务c2b324db-e378-498a-9065-8e4837c0ddc8'   OR name LIKE '%\"zh_CN\":\"性能职务c2b324db-e378-498a-9065-8e4837c0ddc8\"%'   OR name LIKE '%\"zh_TW\":null%'   OR name LIKE '%\"en\":null%') \tAND org_job.tenant_id = -1");
        queryList.add("SELECT count(0) FROM org_job WHERE org_id = -1730833917365171641 \tAND is_enable = 1 \tAND name LIKE concat('%', '性能', '%') ESCAPE '/' \tAND org_job.tenant_id = -1");
        testQuery(queryList.get(0));

        int runTime = 100;
        CountDownLatch downLatch = new CountDownLatch(runTime);

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        long l = System.currentTimeMillis();
        for(int i=0;i<runTime;i++){
            int num = i%queryList.size();
            int finalI = i;
            executorService.submit(()->{
                long startTime = System.currentTimeMillis();
                testQuery(queryList.get(num));
                System.out.println("num:"+ finalI +", "+Thread.currentThread().getName()+"-cost:"+(System.currentTimeMillis()-startTime));
                downLatch.countDown();
            });
        }
        try {
            downLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("all task finished, total cost:"+(System.currentTimeMillis()-l));
        return;
    }


}
